﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

using WebSite.Domain;
using System.Web.UI.WebControls;

namespace WebSite.DAL {
    public class DoencaDAO{

        public static List<Doenca> BuscarDoencasPorProntuario(int idProntuario)
        {
            SqlDataReader dr;
            string strsql = "SELECT *  FROM tab_Doenca where idProntuario = @idProntuario";
            SqlConnection objConnection = ConnectionFactory.obterConexao();

            List<Doenca> doencas;

            try
            {
                objConnection.Open();
                SqlCommand objCommand = new SqlCommand(strsql, objConnection);
                objCommand.Parameters.Add(new SqlParameter("@idProntuario", idProntuario));

                dr = objCommand.ExecuteReader();

                if (dr.HasRows)
                {
                    doencas = new List<Doenca>();
                    while (dr.Read())
                    {
                        doencas.Add(new Doenca(Convert.ToInt32(dr["idDoenca"]), dr["Nome"].ToString(), dr["Descricao"].ToString(), Convert.ToInt32(dr["idProntuario"])));
                    }
                }
                else
                {
                    doencas = null;
                }

                return doencas;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                objConnection.Close();
            }
        }

        public static Doenca BuscarDoenca(int idDoenca)
        {
            SqlDataReader dr;
            string strsql = "SELECT *  FROM tab_Doenca where idDoenca = @idDoenca";
            SqlConnection objConnection = ConnectionFactory.obterConexao();

            Doenca doenca;

            try
            {
                objConnection.Open();
                SqlCommand objCommand = new SqlCommand(strsql, objConnection);
                objCommand.Parameters.Add(new SqlParameter("@idDoenca", idDoenca));

                dr = objCommand.ExecuteReader();

                if (dr.HasRows)
                {
                    dr.Read();
                    doenca = new Doenca(Convert.ToInt32(dr["idDoenca"]), dr["Nome"].ToString(), dr["Descricao"].ToString(), Convert.ToInt32(dr["idProntuario"]));                    
                }
                else
                {
                    doenca = null;
                }

                return doenca;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                objConnection.Close();
            }
        }

        public static List<Doenca> BuscarDoencasDoProntuarioPorCaso(int idCasoPaciente)
        {            
            SqlDataReader dr;
            string strsql = "select * " +
                            "from tab_Doenca " +
                            "where idProntuario = (select idProntuario from tab_CasoPaciente where idCasoPaciente = @idCasoPaciente)";
            SqlConnection objConnection = ConnectionFactory.obterConexao();

            List<Doenca> doencas;

            try
            {
                objConnection.Open();
                SqlCommand objCommand = new SqlCommand(strsql, objConnection);
                objCommand.Parameters.Add(new SqlParameter("@idCasoPaciente", idCasoPaciente));

                dr = objCommand.ExecuteReader();

                if (dr.HasRows)
                {
                    doencas = new List<Doenca>();
                    while (dr.Read())
                    {
                        doencas.Add(new Doenca(Convert.ToInt32(dr["idDoenca"]), dr["Nome"].ToString(), dr["Descricao"].ToString(), Convert.ToInt32(dr["idProntuario"])));
                    }
                }
                else
                {
                    doencas = null;
                }

                return doencas;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                objConnection.Close();
            }
        }

        public static void CadastrarDoenca(Doenca doenca)
        {
            string strsql = "INSERT INTO tab_Doenca(Nome, Descricao,idProntuario) VALUES (@Nome,@Descricao,@idProntuario)";
            SqlConnection objConnection = ConnectionFactory.obterConexao();
            objConnection.Open();
            SqlCommand objCommand = new SqlCommand(strsql, objConnection);
            objCommand.Parameters.Add(new SqlParameter("@Nome", doenca.nome));
            objCommand.Parameters.Add(new SqlParameter("@Descricao", doenca.descricao));
            objCommand.Parameters.Add(new SqlParameter("@idProntuario", doenca.idProntuario));
            objCommand.ExecuteNonQuery();
            objConnection.Close();
        }

        public static Doenca BuscarUltimaDoenca()
        {
            SqlDataReader dr;
            // \/ Problem Here - Trazer ultima doença cadastrada
            string strsql = "select IDENT_CURRENT('SistemaProntuario.dbo.tab_Doenca')";
            SqlConnection objConnection = ConnectionFactory.obterConexao();

            Doenca doenca;

            try
            {
                objConnection.Open();
                SqlCommand objCommand = new SqlCommand(strsql, objConnection);                

                dr = objCommand.ExecuteReader();

                if (dr.HasRows)
                {
                    dr.Read();
                    doenca = new Doenca(Convert.ToInt32(dr["idDoenca"]), dr["Nome"].ToString(), dr["Descricao"].ToString(), Convert.ToInt32(dr["idProntuario"]));
                }
                else
                {
                    doenca = null;
                }

                return doenca;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                objConnection.Close();
            }

        }


    }
}